<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Course extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cats = DB::table('category')->get(['id','cat_name'])->toArray();
        return view('course.index', ['cats'=>$cats]);
    }

    public function getCourse() 
    {
        $courses = DB::table('course')->paginate(6)->toArray();
        return response()->json([
            'code' => 0,
            'count' => $courses['total'],
            'data' => $courses['data']
        ]);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cats = DB::table('category')->get(['id','cat_name'])->toArray();
        return view('course.add', ['cats'=>$cats]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->only(['course_title', 'course_img', 'cat_id', 'is_on', 'is_special', 'ori_price', 'pro_price', 'intro']);
        $data['study_p'] = \mt_rand(10,99);   // 调用根命名空间下的函数
        $res = DB::table('course')->insert($data);
        if ($res) {
            return returnStatus('success', '添加成功');
        } else {
            return returnStatus('fail', '添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $course = DB::table('course')->where('id', $id)->first();
        $cats = DB::table('category')->get(['id', 'cat_name'])->toArray();
        return view('course.edit', ['course'=>$course, 'cats'=>$cats]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->only(['course_title', 'course_img', 'cat_id', 'is_on', 'is_special', 'ori_price', 'pro_price', 'intro']);
        $res = DB::table('course')->where('id', $id)->update($data);
        if ($res) {
            return returnStatus('success', '修改成功');
        } else {
            return returnStatus('fail', '修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('course')->where('id', $id)->delete();
        if ($res) {
            return returnStatus('success', '删除成功');
        } else {
            return returnStatus('fail', '删除失败');
        }
    }

    public function upload(Request $request) {
        $file = $request->file('file');
        return response()->json(uploadImg($file, 'course'));
    }

    public function upIntroImg(Request $request) {
        $file = $request->file('file');
        $res = uploadImg($file, 'intro');
        return response()->json(['errno' => 0, 'data'=>[$res['url']]]);
        
        
    }
}
